﻿

@{
    //ViewBag.Title = "Index";
    //Layout = "~/Views/Shared/_Layout.cshtml";
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>table 组件综合演示 - Layui</title>
    <meta name="renderer" content="webkit" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
</head>

<body>
    <div style="padding: 16px">

        <div class="layui-card">
            <div class="layui-card-body">
                <h3>产妇列表</h3>
                <span style="color:blue">蓝色：前台已录入</span>    <span style="color:red">红色：回访时间已到</span>   <span style="color:green">绿色：已过预产期一周</span><br>
                <table class="layui-hide" id="testpa" lay-filter="testpa"></table>
            </div>
        </div>
    </div>
    <script type="text/html" id="toolbarDemopa">
        <div class="layui-btn-container" style="width: auto;">
            <div class="layui-form-item" style="position: relative;">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="tel" name="managername" lay-verify="required|phone" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="tel" name="managerphone" lay-verify="required|phone" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <label class="layui-form-label" style="width:90px">家庭住址</label>
                <div class="layui-input-inline">
                    <input type="tel" name="manageraddress" lay-verify="required|phone" autocomplete="off" class="layui-input" style="height: 30px;">
                </div>
                <button class="layui-btn layui-btn-sm" lay-event="search">搜索</button>
                <button class="layui-btn layui-btn-sm" lay-event="add" style="position: fixed;right: 3%;">添加</button>
            </div>
        </div>
    </script>
    <script type="text/html" id="barDemopa">
        <div class="layui-clear-space">
            <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="back">回访</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
        </div>
    </script>
    <script>

        layui.use(['table', 'tree', 'util'], function () {
            var table = layui.table;
            var util = layui.util;
            var tree = layui.tree
                , layer = layui.layer
                , util = layui.util



            //第一个实例
            table.render({
                elem: '#testpa'
                // , height: 400
                , defaultToolbar: [],
                height: "full-100"// 最大高度减去其他容器已占有的高度差
                , toolbar: '#toolbarDemopa'
                , url: '/Parturient/DataList' //数据接口
                , page: true //开启分页
                , limit: 10 //每页默认显示的数量

                //,height: 'full-200' //高度最大化
                , cols: [[ //表头
                    //{ type: 'checkbox', fixed: 'left' }
                    //, { field: 'id', title: 'ID', width: 80 }
                     {
                        field: 'ParturientName', title: '姓名', templet: function (d) {
                            // 提取并转换 BirthTime 和 EntryTime
                            let birthTimestamp = d.BirthTime ? d.BirthTime.replace('/Date(', '').replace(')/', '') : '';
                             let entryTimestamp = d.EntryTime ? d.EntryTime.replace('/Date(', '').replace(')/', '') : '';

                             if (d.IsDelete == 1) return `<span style="color: blue;">${d.ParturientName}</span>`;
                            if (birthTimestamp && !isNaN(birthTimestamp) && entryTimestamp && !isNaN(entryTimestamp)) {
                                let birthDate = new Date(parseInt(birthTimestamp));
                                let entryDate = new Date(parseInt(entryTimestamp));
                                
                                // 计算出生日期前后各一周的时间区间
                                let oneWeekBefore = new Date(birthDate);
                                oneWeekBefore.setDate(birthDate.getDate() - 7);

                                let oneWeekAfter = new Date(birthDate);
                                oneWeekAfter.setDate(birthDate.getDate() + 7);
                                if (Date.now() > oneWeekAfter.getTime()) return `<span style="color: green;">${d.ParturientName}</span>`;
                                if (d.Weight < 3) return d.ParturientName
                                // 判断 EntryTime 是否在 BirthTime 前后的一周区间内
                                if (entryDate < oneWeekBefore) return d.ParturientName;
                                if (Date.now()>= oneWeekBefore.getTime()&& Date.now()<= oneWeekAfter.getTime()) {
                                    // 如果 EntryTime 与 BirthTime 的间隔小于等于两天，姓名变红
                                    let diffInTime = Math.abs(Date.now() - entryDate.getTime());
                                    let diffInDays = diffInTime / (1000 * 3600 * 24); // 转换为天数
                                    if (diffInDays >= 2) {
                                        return `<span style="color: red;">${d.ParturientName}</span>`;
                                    }   
                                }
                            } return d.ParturientName
                        }
                    }
                    , { field: 'ParturientAge', title: '年龄' }
                    , { field: 'ParturientPhone', title: '电话' }
                    , { field: 'Address', title: '家庭地址' }
                    , { field: 'Degree', title: '学历' }
                    , { field: 'EmergencyContacts', title: '紧急联系人' }
                    , { field: 'EmergencyContactsPhone', title: '紧急联系人电话' }
                    , { field: 'PerinatalHospitals', title: '围产医院' }
                    , { field: 'Salesman', title: '业务员' }
                    , { field: 'SalesmanNumbers', title: '业务员工号' }
                    , {
                        field: 'BirthTime', title: '预产期',
                        templet: d => {
                            // 提取并转换时间
                            let timestamp = d.BirthTime ? d.BirthTime.replace('/Date(', '').replace(')/', '') : '';
                            if (timestamp && !isNaN(timestamp)) {
                                let date = new Date(parseInt(timestamp));
                                return util.toDateString(date, 'yyyy-MM-dd HH:mm:ss');
                            }
                            return '';  // 如果 CreateTime 不合法，则返回空字符串
                        }
                    }
                    , {
                        field: 'CreateTime', title: '录入时间',
                        templet: d => {
                            // 提取并转换时间
                            let timestamp = d.CreateTime ? d.CreateTime.replace('/Date(', '').replace(')/', '') : '';
                            if (timestamp && !isNaN(timestamp)) {
                                let date = new Date(parseInt(timestamp));
                                return util.toDateString(date, 'yyyy-MM-dd HH:mm:ss');
                            }
                            return '';  // 如果 CreateTime 不合法，则返回空字符串
                        }
                    }
                   // , { field: 'Weights', title: '权重' }
                    , { fixed: 'right', title: '操作', width: 155, minWidth: 155, toolbar: '#barDemopa' }
                ]]
            });
            table.on('toolbar(testpa)', function (obj) {
                if (obj.event === 'add') {
                    //console.log(obj)
                    //var checkStatus = table.checkStatus(obj.config.id);
                    //var data = checkStatus.data;
                    //console.log(data)
                    layer.open({
                        title: '添加',
                        type: 2,
                        area: ['80%', '80%'],
                        content: '/Parturient/Add'
                    });
                }
                else if (obj.event === 'search') {
                    var phone = $('input[name=managerphone]').val();
                    var name = $('input[name=managername]').val();
                    var address = $('input[name=manageraddress]').val();
                    //var checkStatus = table.checkStatus(obj.config.id);
                    //var data = checkStatus.data;
                    //console.log(data)
                    table.reload('testpa', {
                        where: {
                            // 这里的参数是请求接口的参数
                            // search: $('#search').val()
                            phone,
                            address,
                            name
                        }
                    });
                    $('input[name=managerphone]').val(phone);
                    $('input[name=managername]').val(name);
                    $('input[name=manageraddress]').val(address);
                }
            });
            table.on('tool(testpa)', function (obj) { // 双击 toolDouble
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        obj.del();
                        del(data.ParturientName);
                        layer.close(index);
                    });
                }
                else if (obj.event === 'edit') {
                    layer.open({
                        title: '编辑',
                        type: 2,
                        area: ['80%', '80%'],
                        content: '/Parturient/Add?name=' + data.ParturientName,
                    });
                }
                else if (obj.event === 'back') {
                    layer.open({
                        title: '回访',
                        type: 2,
                        area: ['80%', '80%'],
                        content: '/Parturient/Revisit?name=' + data.ParturientName,
                    });
                }
            });
            //删除
            function del(ParturientName) {
                $.post("/Parturient/Delete", { name: ParturientName }, function (resp) {
                    if (resp.code == 0) {
                        layer.msg(resp.msg);
                        table.reload('testpa');
                    }
                })
            }
        });
    </script>

</body>
</html>
